package com.example.generator.controller;

import com.example.generator.base.GenConfig;
import com.example.generator.base.GeneratorConfig;
import com.example.generator.entity.TableInfo;
import com.example.generator.service.IGeneratorService;
import com.example.util.PageResult;
import com.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author Jiamingzhen
 * @Date 2021/4/23 9:10
 * @ClassName GenertorController
 * @Introduce
 */
@Controller
@RequestMapping("/generator")
public class GeneratorController {
    private String PREFIX = "view/generator/";
    @Autowired
    private IGeneratorService generatorService;

    @GetMapping("")
    public String userTable(){
        return PREFIX+"generator";
    }

    @RequestMapping("/generatorTable")
    @ResponseBody
    public PageResult userMessageTable(Integer page, Integer limit, TableInfo tableInfo, HttpSession session){
        return generatorService.queryList(page, limit, tableInfo);
    }
    /**
     * 生成代码
     */
    @RequestMapping(value = "/createGenerate", method = RequestMethod.POST)
    @ResponseBody
    public Result generate(GenConfig genConfig) {
        genConfig.setModuleName(genConfig.getTableName());
        GeneratorConfig webGeneratorConfig = new GeneratorConfig(genConfig);
        try {
            webGeneratorConfig.doMpGeneration();
        }catch (Exception e) {
            return Result.error(500,"代码生成失败");
        }
        return Result.success(200,"代码生成成功");
    }
}
